<?php
/*
 * BorderDAO
 * 
 */
 
require("com/borderwaitmaps/util/Constants.php");
require("com/borderwaitmaps/entities/BorderCrossing.php");
require("com/borderwaitmaps/entities/BorderCrossings.php");
require("com/borderwaitmaps/entities/VehicleFlow.php");
require("com/borderwaitmaps/entities/Lane.php");
require("com/borderwaitmaps/entities/Location.php");


class BorderDAO
{
	
	var $WAIT_TIMES_URL = "http://www.cbsa-asfc.gc.ca/general/times/menu-e.html";
	var $WAIT_TIMES_PATTERN_ROW = "/<TR><TD headers=\"Office\"/";
	var $WAIT_TIMES_PATTERN_MATCH = "/<TR><TD headers=\"Office\" VALIGN=\"TOP\"><P><STRONG>(.*)<\/STRONG><br>(.*)<br><span class=\"m\">Last updated:<br><strong>(.*)<\/strong><\/span><\/p><\/TD><TD headers=\"Com ComCanada\" VALIGN=\"TOP\">(.*)<\/TD><TD headers=\"Com ComUS\" VALIGN=\"TOP\">(.*)<\/TD><TD headers=\"Trav TravCanada\" VALIGN=\"TOP\">(.*)<\/TD><TD headers=\"Trav TravUS\" VALIGN=\"TOP\">(.*)<\/TD><\/TR>/";
	
	var $COMMERCIAL_FLOW = "Commercial";
	var $TRAVELLER_FLOW = "Traveller";
	
	var $DIRECTION_CANADA = "Canada";
	var $DIRECTION_US = "U.S.";

	function BorderDAO()
	{
		
	}
	
	function getBorderCrossings()
	{
		
		/*
		 * TODO: Hook up this call to a MySQL DB
		 * In the meantime, serve back some canned data
		 */

		$bc = new BorderCrossing();
		$bc->setName("Sample Crossing");
		$bc->setCountry("UNITED_STATES");
		$bc->setAlternateCrossingName("Alt Name");
		$bc->setHours("9-12");
		
		// set location
		$location = new Location();
		$location->setCity("My City");
		$location->setLatitude("40.23456");
		$location->setLongitude("111.123455");
		$bc->setLocation($location);
		
		// set commercial flow
		$commFlow = new VehicleFlow();
			$commLane1 = new Lane();
			$commLane1->setName("Standard");
			$commLane1->setDelay("12");
			$commFlow->setPrimaryLane($commLane1);
			
			$commLane2 = new Lane();
			$commLane2->setName("Fast");
			$commLane2->setDelay("8");
			$commFlow->setSecondaryLane($commLane2);
		$bc->setCommercialVehicleFlow($commFlow);	
		
		// set passenger flow
		$passFlow = new VehicleFlow();	
			$passLane1 = new Lane();
			$passLane1->setName("Standard");
			$passLane1->setDelay("4");
			$passFlow->setPrimaryLane($passLane1);
			
			$passLane2 = new Lane();
			$passLane2->setName("Nexus");
			$passLane2->setDelay("9");
			$passFlow->setSecondaryLane($passLane2);
		$bc->setPassengerVehicleFlow($passFlow);		 
		
		
		// second one, just use other data
		$bc2 = new BorderCrossing();
		$bc2->setName("Sample Crossing #2");
		$bc2->setCountry("CANADA");
		$bc2->setLocation($location);
		$bc2->setCommercialVehicleFlow($commFlow);
		$bc2->setPassengerVehicleFlow($passFlow);
		
		$crossings = array();
		array_push($crossings, $bc);
		array_push($crossings, $bc2);
		
		// create a container
		$borderCrossings = new BorderCrossings();
		$borderCrossings->setCrossings($crossings);
		return $borderCrossings;
		
	}
	
	function getCustomsOffices()
	{
		$offices = $this->parsePage();
		return $offices;
	}
	
	function parsePage()
	{
		$lines = file($this->WAIT_TIMES_URL);
		$offices = array();
		foreach ($lines as $line_num => $line)
		{
			$pattern = $this->WAIT_TIMES_PATTERN_ROW;
			if( preg_match($pattern, $line, &$officeMatches) )
			{
				$office = $this->parseRow($line);
				array_push($offices, $office);
			}
					
		}
		return $offices;
		
	}
	
	/*
	function parseRow($row)
	{ 
		$pattern = $this->WAIT_TIMES_PATTERN_MATCH;
		preg_match($pattern, $row, $matches);
		
		// These matches are hard coded to the WAIT_TIMES_PATTERN
		// If the pattern changes, the number of matches may change
		$officeName = $matches[1];
		$crossing = $matches[2];
		$lastUpdate = $matches[3];
		$commercialFlowWaitTimeCanadaBound = $matches[4];
		$commercialFlowWaitTimeUSBound = $matches[5];
		$travellersFlowWaitTimeCanadaBound = $matches[6];
		$travellersFlowWaitTimeUSBound = $matches[7];
		
		// create an office
		$office = new CustomsOffice();
		//print "office name=" . $officeName;
		$office->setName($officeName);
		
		// we need to split crossing on " / " for Canadian and American cities
		// array split ( string $pattern, string $string [, int $limit] )
		$cities = split( " / ", $crossing);
		$canadianCityName = $cities[0];
		$americanCityName = $cities[1];
		
		$canadianCity = new City();
		$canadianCity->setName($canadianCityName);
		$canadianCityGeoCoding = $this->getCoordinates($canadianCityName);  // $this->getGeoCoding($canadianCityName);
		$canadianCity->setGeoCoding($canadianCityGeoCoding);
		$office->setCanadianCity( $canadianCity );
		
		$americanCity = new City();
		$americanCity->setName($americanCityName);
		$americanCityGeoCoding = $this->getCoordinates($americanCityName);  //  $this->getGeoCoding($americanCityName);
		$americanCity->setGeoCoding($americanCityGeoCoding);
		$office->setAmericanCity( $americanCity );
		
		$office->setLastMod( $lastUpdate );
		
		// create two Flow objects
		$commercialFlow = new Flow($this->COMMERCIAL_FLOW);
		$travellerFlow = new Flow($this->TRAVELLER_FLOW);
		
		// create wait times
		
		$cwt_com = new WaitTime();
		$cwt_com->setDirection( $this->DIRECTION_CANADA );
		$cwt_com->setDelay( $commercialFlowWaitTimeCanadaBound );
		$commercialFlow->addWaitTime( $cwt_com );
		
		$cwt_trav = new WaitTime();
		$cwt_trav->setDirection( $this->DIRECTION_CANADA );
		$cwt_trav->setDelay( $travellersFlowWaitTimeCanadaBound );
		$travellerFlow->addWaitTime( $cwt_trav );
		
		$uswt_com = new WaitTime();
		$uswt_com->setDirection( $this->DIRECTION_US );
		$uswt_com->setDelay( $commercialFlowWaitTimeUSBound );
		$commercialFlow->addWaitTime( $uswt_com );
		
		$cwt_trav = new WaitTime();
		$cwt_trav->setDirection( $this->DIRECTION_US );
		$cwt_trav->setDelay( $travellersFlowWaitTimeUSBound );
		$travellerFlow->addWaitTime( $cwt_trav );
		
		$office->setCommercialFlow($commercialFlow);
		$office->setTravellersFlow($travellerFlow);
		
		return $office;
		
	}
	
	function getGeoCoding($place)
	{
		// convert spaces in place to plus signs
		$query = preg_replace("/ /", "+", $place);
		$geoquery = "http://maps.google.com/maps/geo?q=$query&output=text&key=ABQIAAAAGt86K09TmfKzrelwDOWGFhR-Zr9lOu2P1JTNfOIzTcWT167w4BSESv7Tc9tsdjTbufu35g_NlGneJA";
		
		// fetch the geocoded info
		$geocoding = file($geoquery);
		print "\n<hr>";
		print_r($geocoding);
		print "\n";
		return $geocoding;	
	}

	function getCoordinates($cityName)
	{
		$coords = array(
			"St. Stephen, NB" => "45.192442,-67.283492",
			"Calais, ME" => "45.190566,-67.282891",
			"Belleville, NB" => "46.13815495,-67.77770519",
			"Houlton, ME" => "46.13405109,-67.78304815",
			"Stanstead, QC" => "45.00648823,-72.08775759",
			"Derby Line, VT" => "45.00462215,-72.0896244",
			"St. Armand, QC" => "45.01715256,-73.08457375",
			"Highgate Springs, VT" => "45.01248052,-73.08609724",
			"Lacolle, QC" => "45.01059946,-73.45227242",
			"Champlain, NY" => "45.00556278,-73.45321655",
			"Cornwall, ON" => "44.99903871,-74.73992586",
			"Rooseveltown, NY" => "44.98244451,-74.73941088",
			"Lansdowne, ON" => "44.3479363,-75.98382175",
			"Alexandria Bay, NY" => "44.3446448,-75.98171353",
			"Queenston, ON" => "43.15504262,-79.04993534",
			"Lewiston, NY" => "43.15180226,-79.03633118",
			"Niagara Falls, ON" => "43.09210669,-79.06977296",
			"Niagara Falls, NY" => "43.08842814,-79.0651381",
			"Fort Erie, ON" => "42.90792432,-78.91288519",
			"Buffalo, NY" => "42.90363353,-78.8989377",
			//"Windsor, ON (D-C Tunnel)" => "42.31542461,-83.0363524", //Detroit-Canada Tunnel
			//"Detroit, MI (D-C Tunnel)" => "42.3284341,-83.04187775", //Detroit-Canada Tunnel
			"Windsor, ON" => "42.30140466,-83.06573868", //Ambassador Bridge
			"Detroit, MI" => "42.32089051,-83.0788064", //Ambassador Bridge
			"Sarnia, ON" => "42.99306537,-82.41175175",
			"Port Huron, MI" => "42.99847988,-82.43638515",
			"Sault Ste. Marie, ON" => "46.51936292,-84.34939027",
			"Sault Ste. Marie, MI" => "46.49505393,-84.36755419",
			"Fort Frances, ON" => "48.60885903,-93.40036511",
			"International Falls, MN" => "48.6055248,-93.40241432",
			"Emerson, MB" => "49.00112599,-97.21023917",
			"Pembina, ND" => "49.00014057,-97.20621586",
			"North Portal, SK" => "48.99939798,-102.5536394",
			"Portal, ND" => "48.99859203,-102.551955",
			"Coutts, AB" => "48.99894046,-111.9607472",
			"Sweet Grass, MT" => "48.99787405,-111.9597065",
			"Surrey, BC" => "49.00304749,-122.7344835", //Pac Highway
			"Blaine, WA" => "49.00176649,-122.7359158", //Pac Highway
			//"Surrey, BC (Peace Arch)" => "49.0041173,-122.7572393", //Douglas - Peace Arch
			//"Blaine, WA (Peace Arch)" => "49.00029895,-122.7548146", //Douglas - Peace Arch
			"Delta, BC" => "49.00233309,-123.0680698",
			"Point Roberts, WA" => "49.0011823,-123.0678713",
			"Huntingdon, BC" => "49.00293487,-122.2647643",
			"Sumas, WA" => "49.00188615,-122.2654241"
			
		);
		
		return $coords[$cityName];
		
	}	
	*/
	
	function __toString()
	{
		return get_class($this);
	}
	
}


/*

 */
?>
